package cn.edu.ctbu.sbadmin.lablog.webapi;

import cn.edu.ctbu.sbadmin.common.controller.BaseRestController;
import cn.edu.ctbu.sbadmin.common.utils.MQuery;
import cn.edu.ctbu.sbadmin.common.utils.MQueryHelper;
import cn.edu.ctbu.sbadmin.common.utils.PageUtils;
import cn.edu.ctbu.sbadmin.common.utils.R;
import cn.edu.ctbu.sbadmin.lablog.domain.DimLabDO;
import cn.edu.ctbu.sbadmin.lablog.service.DimLabService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("webapi/lablog/dimlab")
public class DimLabRestController extends BaseRestController {


    @Autowired
    DimLabService dimLabService;

    @RequestMapping("/getByPage")
    @RequiresPermissions("lablog:dimlab:list")
    public PageUtils list(@RequestParam("where") String where,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("pageIndex") Integer pageIndex,
                          @RequestParam("sort") String sort,
                          @RequestParam("direct") String direct
    ) throws Exception {
        //首先，把where条件的参数转为QueryAndItems
//        其中,where条件需要我们自己组装.示例:
//        name=张^:like&time=2^:l g&dmtCreate=2017-01-01^^2018-01-01
//        这个表示:
//        (name like '%张%' )and ( time<2 ) and (dmtCreate between '2017-01-01' and '2018-01-01)
        MQuery mQuery= MQueryHelper.GenQuery(where,pageSize,pageIndex,sort,direct);

        PageInfo<DimLabDO> DimLabDOPageInfo = dimLabService.getByPage(mQuery);

        PageUtils pageUtils = new PageUtils(DimLabDOPageInfo.getList(),DimLabDOPageInfo.getTotal());

        return pageUtils;
    }

    @RequestMapping("/get")
    @RequiresPermissions("lablog:dimlab:list")
    public R get(Long id){
        DimLabDO DimLabDO=dimLabService.get(id);
        if(id!=null)
            return R.ok("data",DimLabDO);
        return R.error(-1,"找不到数据");
    }

    @RequestMapping("/save")
    @RequiresPermissions("lablog:dimlab:add")
    public R save(DimLabDO DimLabDO){
        if(dimLabService.save(DimLabDO)>0)
            return R.ok("data",DimLabDO);
        return R.error();

    }

    @RequestMapping("/update")
    @RequiresPermissions("lablog:dimlab:edit")
    public R update(DimLabDO DimLabDO){
        if(dimLabService.update(DimLabDO)>0){
            return R.ok("data",DimLabDO);
        }
        return R.error();
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions("lablog:dimlab:remove")
    public R batchDelete(@RequestParam("ids[]") Long[] ids){
        List<DimLabDO> DimLabDOS=dimLabService.findByIds(ids);
        if(DimLabDOS.size()>0){
            for(DimLabDO DimLabDO:DimLabDOS){
                dimLabService.delete(DimLabDO.getId());
            }
        }
        return R.ok();
    }


    @RequestMapping("/getAll")
    @RequiresPermissions("lablog:dimlab:list")
    public R getAll() throws Exception {
        List<DimLabDO> dimLabDOS=dimLabService.findAll();
       return R.ok("data",dimLabDOS);
    }
}
